package com.kecheng.action;

import com.kecheng.dao.AdminDao;
import com.kecheng.dao.TongjiDao;
import com.kecheng.pojo.Admin;
import com.kecheng.pojo.Tongji;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 后台用户模块
 *
 * @author
 */


@Action(value = "shezhi", results = {
        @Result(name = "success", location = "/admin/index.jsp"),
        @Result(name = "test", location = "/index.jsp"),
        @Result(name = "fail", type = "redirect",location = "/admin/login.jsp"),
        @Result(name = "test", location = "/index.jsp"),
        @Result(name = "login", location = "/admin/login.jsp"),
})
@AllowedMethods(value = {("regex:.*")})
public class ShezhiAction extends BaseAction {

    private Admin admin;
    private List<Admin> admins;
    private File file;
    private String fileFileName;
    private String fileContentType;

    @Autowired
    private TongjiDao tongjiDao;

    @Autowired
    private AdminDao adminDao;
    private Tongji tongji;
    public int ss = 100;

    public String toLogin(){
        return "login";
    }
    @Transactional
    public String login() {
        String r = null;
        try {
            Admin admin1 = adminDao.findbyname(admin.getName());
            if (admin1 == null) {
                msg = "fail";
                r = "fail";

            } else if (admin1.getPassword().equals(admin.getPassword())) {
                int num = admin1.getNum() + 1;
                admin1.setNum(num);
                SimpleDateFormat df = new SimpleDateFormat(
                        "yyyy-MM-dd HH:mm:ss");
                admin1.setLasttime(admin1.getThistime());
                admin1.setThistime(df.format(new Date()));
                Date d = new Date();

                SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
                tongji = tongjiDao.findbytime(df1.format(d.getTime() - 1 * 24 * 60 * 60 * 1000));
                session.put("tongji", tongji);
                session.put("admin", admin1);
//				HttpSession session = HttpServletRequest.getSession();
//				session.setAttribute("admin", admin1);
//				session.setMaxInactiveInterval(7200);
                r = "success";
            } else {
                msg = "fail";
                r = "fail";
            }
        } catch (Exception e) {
            e.printStackTrace();
            msg = "fail";
        }
        return r;
    }

    public String update() throws IOException {
        if (file == null) {
            try {

                Admin admin1 = adminDao.findbyid(admin.getId());
                System.out.println(admin1);
                admin.setImg(admin1.getImg());
                adminDao.update(admin);
            } catch (Exception e) {
                e.printStackTrace();
            }

            return "login";

        } else {

            String root = ServletActionContext.getServletContext().getRealPath(
                    "/upload");
            InputStream is = new FileInputStream(file);
            OutputStream os = new FileOutputStream(new File(root, fileFileName));
            byte[] buffer = new byte[500];
            int length = 0;
            while (-1 != (length = is.read(buffer, 0, buffer.length))) {
                os.write(buffer);
            }

            os.close();
            is.close();

            admin.setImg("upload/" + fileFileName);

            adminDao.update(admin);

            return "login";

        }

    }

    public Admin getAdmin() {
        return admin;
    }

    public void setAdmin(Admin admin) {
        this.admin = admin;
    }

    public List<Admin> getAdmins() {
        return admins;
    }

    public void setAdmins(List<Admin> admins) {
        this.admins = admins;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String getFileFileName() {
        return fileFileName;
    }

    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }

    public String getFileContentType() {
        return fileContentType;
    }

    public void setFileContentType(String fileContentType) {
        this.fileContentType = fileContentType;
    }

    public Tongji getTongji() {
        return tongji;
    }

    public void setTongji(Tongji tongji) {
        this.tongji = tongji;
    }


}
